import { getWbiKeys,encWbi } from '@/api/getVideo'
import { randomId } from '@/api/qv_id'
import { useStore } from 'vuex'
let video_query = {
    page_size:42,
    order:'',
    order_sort:'',
    duration:'',
    ad_resource:5654,
    category_id:'',
    dynamic_offset:0,
    search_type:'video'
}
let live_query = {
    page_size:42,
    order:'online',
    order_sort:0,
    duration:'',
    ad_resource:'',
    category_id:'',
    source_tag:5,
    dynamic_offset:0,
    search_type:'live',
}
let media_ft_query = {
    page_size:12,
    order:'',
    order_sort:0,
    duration:'',
    ad_resource:'',
    category_id:'',
    dynamic_offset:'',
    search_type:'media_ft',
}
let media_bangumi_query = {
    page_size:12,
    order:'',
    order_sort:0,
    duration:'',
    ad_resource:'',
    category_id:'',
    dynamic_offset:'',
    search_type:'media_bangumi',
}
let article_query = {
    page_size:20,
    order:'toralrank',
    order_sort:0,
    duration:'',
    ad_resource:'',
    category_id:0,
    dynamic_offset:'',
    single_column:1,
    search_type:'article',
}
let bili_user_query = {
    page_size: 36,
    order:'',
    order_sort:0,
    duration:'',
    ad_resource:'',
    category_id:'',
    dynamic_offset:0,
    search_type:'bili_user',
    user_type:0
}
let public_qarams = {
    _refresh_:true,
    _extra:'',
    context:'',
    page:1,
    //order:'',         none    /online /''          /''                /totalrank/''
                        //最多播放click/最新发布pubdate/最多弹幕dm/最多收藏stow
    from_source:'',
    from_spmid:'333.337',
    platform:'pc',
    highlight:1,
    single_column:0,
    source_tag:3,
    gaia_vtoken:'',
    web_location:1430654
}
let all_query = {
    _refresh_:true,
    _extra:'',
    context:'',
    page:1,
    page_size:42,
    order:'',
    duration:'',
    from_source:'',
    from_spmid:'333.337',
    platform:'pc',
    highlight:1,
    single_column:0,

    ad_resource:5646,
    source_tag:3,
    web_location:1430654,
}

const map = new Map()
map.set('video',video_query)
map.set('live',live_query)
map.set('media_ft',media_ft_query)
map.set('media_bangumi',media_bangumi_query)
map.set('article',article_query)
map.set('user',bili_user_query)
map.set('all',all_query)

type change_query = {
    order:string
}

/**
 * 
 * @param type 需要展示数据的类型 综合/视频/用户...
 * @return 尾调用 返回经过加工的json字符串方便接在http请求地址后
 * 
 * 使用扩展运算符进行扩展时 若后面的属性与前面的属性重复时 会直接覆盖
 * 这样的设计也就使得 可以用来对一些后端接口的部分参数进行更换 获得不同的数据
 */
export function getQuery(type:string,keyword:string,other?:change_query) :Promise<string>{
    const store = useStore()
    const wbi_keys = getWbiKeys(store)    
    let qv_id = randomId( 32,'0-9a-zA-Z')
    return  encWbi(
                    {
                        qv_id,
                        keyword,
                        ...public_qarams,
                        ...map.get(type),
                        ...other
                    },
                    wbi_keys.img_key, 
                    wbi_keys.sub_key
                )
                
}